package com.ruoyi.project.modules.errorCode.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.errorCode.domain.ErrorCode;
import com.ruoyi.project.modules.errorCode.service.IErrorCodeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 错误码Controller
 *
 * @author smallrain
 * @date 2022-04-04
 */
@Controller
@RequestMapping("/errorCode/errorCode")
public class ErrorCodeController extends BaseController {
    private String prefix = "errorCode/errorCode";

    @Autowired
    private IErrorCodeService errorCodeService;

    @RequiresPermissions("errorCode:errorCode:view")
    @GetMapping()
    public String errorCode() {
        return prefix + "/errorCode";
    }

    /**
     * 查询错误码列表
     */
    @RequiresPermissions("errorCode:errorCode:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ErrorCode errorCode) {
        startPage();
        List<ErrorCode> list = errorCodeService.selectErrorCodeList(errorCode);
        return getDataTable(list);
    }

    /**
     * 根据分类编码，查询错误码
     * @author chenxiaoyu
     * @date 2022/4/6 上午1:41
     * @param errorCodeCategoryCode
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryErrorCode")
    @ResponseBody
    public AjaxResult queryErrorCode(String errorCodeCategoryCode) {
        ErrorCode errorCode = new ErrorCode();
        errorCode.setErrorCodeCategoryCode(errorCodeCategoryCode);
        List<ErrorCode> list = errorCodeService.selectErrorCodeList(errorCode);
        return AjaxResult.success(list);
    }

    /**
     * 根据分类编码，查询错误码
     * @author chenxiaoyu
     * @date 2022/4/6 上午1:41
     * @param errorCodeCategoryId
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryErrorCodeById")
    @ResponseBody
    public AjaxResult queryErrorCodeById(Long errorCodeCategoryId) {
        ErrorCode errorCode = new ErrorCode();
        errorCode.setErrorCodeCategoryId(errorCodeCategoryId);
        List<ErrorCode> list = errorCodeService.selectErrorCodeList(errorCode);
        return AjaxResult.success(list);
    }

    /**
     * 导出错误码列表
     */
    @RequiresPermissions("errorCode:errorCode:export")
    @Log(title = "错误码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ErrorCode errorCode) {
        List<ErrorCode> list = errorCodeService.selectErrorCodeList(errorCode);
        ExcelUtil<ErrorCode> util = new ExcelUtil<ErrorCode>(ErrorCode.class);
        return util.exportExcel(list, "错误码数据");
    }

    /**
     * 新增错误码
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存错误码
     */
    @RequiresPermissions("errorCode:errorCode:add")
    @Log(title = "错误码", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ErrorCode errorCode) {
        return toAjax(errorCodeService.insertErrorCode(errorCode));
    }

    /**
     * 修改错误码
     */
    @GetMapping("/edit/{errorCodeId}")
    public String edit(@PathVariable("errorCodeId") Long errorCodeId, ModelMap mmap) {
        ErrorCode errorCode = errorCodeService.selectErrorCodeByErrorCodeId(errorCodeId);
        mmap.put("errorCode", errorCode);
        return prefix + "/edit";
    }

    /**
     * 修改保存错误码
     */
    @RequiresPermissions("errorCode:errorCode:edit")
    @Log(title = "错误码", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ErrorCode errorCode) {
        return toAjax(errorCodeService.updateErrorCode(errorCode));
    }

    /**
     * 删除错误码
     */
    @RequiresPermissions("errorCode:errorCode:remove")
    @Log(title = "错误码", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(errorCodeService.deleteErrorCodeByErrorCodeIds(ids));
    }
}
